/*
 * FileCopier.java
 *
 * Created on December 2, 2007, 2:55 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package filecopier.gui;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.event.ChangeEvent;
import filecopier.Singleton;
import filecopier.ActionCancelException;
import filecopier.options.Options;
import custom.swing.JStatusBar;

/**
 *
 * @author david
 */
public class FileCopier extends JFrame {

    private MainTabbedPane mainTabbedPane = null;
    private ButtonPanel buttonPanel = null;
    private JStatusBar statusBar = null;
    private HelpDialog helpDialog = null;
    private AboutDialog aboutDialog = null;

    /** Creates a new instance of FileCopier */
    public FileCopier(final String title) {
        super(title);
        Singleton.getInstance().getFileAlwaysLogger().info("FileCopier starting in GUI mode.");
        initialize();
    }

    private void initialize() {
        this.setContentPane(getJContentPane());
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setIconImage(Singleton.getInstance().getImage16());
        this.pack();
        this.setVisible(true);
        this.setBounds(Singleton.getInstance().getOptions().getBounds());

        Singleton.getInstance().setFrame(this);
        javax.swing.ToolTipManager.sharedInstance().setDismissDelay(20000);

        this.addComponentListener(new java.awt.event.ComponentAdapter() {

            @Override
            public void componentShown(java.awt.event.ComponentEvent e) {
                Dimension minSize = getMinimumSize();
                Dimension actualSize = getSize();

                if (actualSize.height < minSize.height || actualSize.width < minSize.width) {
                    setSize(minSize);
                }
            }
        });

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent we) {
                dispose();
            }

            @Override
            public void windowClosed(WindowEvent we) {
                handleClose();
            }
            
            public void handleClose() {
                Singleton singleton = Singleton.getInstance();
                singleton.getOptions().storeProperties(getSize(), getLocation(), getExtendedState());
                singleton.saveLog4jFile();
                if(helpDialog != null) helpDialog.dispose();
                if(aboutDialog != null) aboutDialog.dispose();
            }
        });
    }
    
    private JPanel getJContentPane() {
        final JPanel contentPane = new JPanel(new java.awt.BorderLayout());
        Box centerBox = new Box(BoxLayout.Y_AXIS);
        centerBox.add(getMainTabbedPane());
        centerBox.add(getButtonPanel());
        contentPane.add(centerBox, java.awt.BorderLayout.CENTER);
        contentPane.add(getStatusBar(), java.awt.BorderLayout.SOUTH);
        contentPane.setOpaque(true);
        return contentPane;
    }

    public MainTabbedPane getMainTabbedPane() {
        if (mainTabbedPane == null) {
            mainTabbedPane = new MainTabbedPane();
            mainTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {

                public void stateChanged(ChangeEvent e) {
                    buttonPanel.actionBtn.setText(buttonPanel.actionBtnTxt[mainTabbedPane.getSelectedIndex()]);
                    buttonPanel.actionBtn.setMnemonic(buttonPanel.actionMnemonics[mainTabbedPane.getSelectedIndex()]);
                }
            });
        }
        return mainTabbedPane;
    }

    private ButtonPanel getButtonPanel() {
        if (buttonPanel == null) {
            buttonPanel = new ButtonPanel();
        }
        return buttonPanel;
    }

    private JStatusBar getStatusBar() {
        if (statusBar == null) {
            statusBar = new JStatusBar(3);
            java.awt.Color color = statusBar.getBackground().darker();
            statusBar.setBorder(new javax.swing.border.MatteBorder(new java.awt.Insets(1, 1, 1, 1), color.darker()));
            statusBar.setMessage("Ready", 0);
        }

        return statusBar;
    }
    
    class ButtonPanel extends JPanel {

        private JButton actionBtn = null;
        private JButton helpBtn = null;
        private JButton aboutBtn = null;
        private JButton closeBtn = null;
        private final String actionBtnTxt[] = new String[]{"Copy", "Restore", "Use Defaults", "Use Defaults", "Use Defaults"};
        private final int[] actionMnemonics = new int[]{java.awt.event.KeyEvent.VK_C, java.awt.event.KeyEvent.VK_R,
            java.awt.event.KeyEvent.VK_U, java.awt.event.KeyEvent.VK_U, java.awt.event.KeyEvent.VK_U
        };

        public ButtonPanel() {
            super();
            initialize();
        }

        private void initialize() {
            this.setLayout(new java.awt.GridLayout(1, 4, 2, 2));
            this.add(getActionBtn());
            this.add(getHelpBtn());
            this.add(getAboutBtn());
            this.add(getCloseBtn());
            this.setMaximumSize(new Dimension(Short.MAX_VALUE, this.getPreferredSize().height));
        }

        private JButton getActionBtn() {
            if (actionBtn == null) {
                int idx = mainTabbedPane.getSelectedIndex();
                actionBtn = new JButton(actionBtnTxt[idx]);
                actionBtn.setMnemonic(actionMnemonics[idx]);
                actionBtn.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(java.awt.event.ActionEvent ae) {
                        Options.TabIndex tabIndex = Singleton.getInstance().getOptions().getTabIndex();
                        int idx = tabIndex.ordinal();
                        try {
                            mainTabbedPane.getActionCommand(tabIndex.ordinal()).doAction();
                        } catch (ActionCancelException e) {
                            // Log the exception.
                            Singleton.getInstance().getFileLogger().error(e);
                            if (idx == Options.TabIndex.Copy.ordinal() || idx == Options.TabIndex.Restore.ordinal()) {
                                // We encountered the exception during a copy/restore operation, so the GUI logger is available.
                                Singleton.getInstance().getFileGuiLogger().error(e);
                            }
                        }
                    }
                });
            }
            return actionBtn;
        }

        private JButton getHelpBtn() {
            if (helpBtn == null) {
                helpBtn = new JButton("Help");
                helpBtn.setMnemonic(java.awt.event.KeyEvent.VK_H);
                helpBtn.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        if(helpDialog == null) {
                            helpDialog = new HelpDialog();
                        } else {
                            helpDialog.setVisible(!helpDialog.isVisible());
                        }
                    }
                });
            }
            return helpBtn;
        }

        private JButton getAboutBtn() {
            if (aboutBtn == null) {
                aboutBtn = new JButton("About");
                aboutBtn.setMnemonic(java.awt.event.KeyEvent.VK_A);
                aboutBtn.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        if(aboutDialog == null) {
                            aboutDialog = new AboutDialog();
                        } else {
                            aboutDialog.setVisible(!aboutDialog.isVisible());
                        }
                    }
                });
                
            }
            return aboutBtn;
        }

        private JButton getCloseBtn() {
            if (closeBtn == null) {
                closeBtn = new JButton("Close");
                closeBtn.setMnemonic(java.awt.event.KeyEvent.VK_S);
                closeBtn.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        dispose();
                    }
                });
            }
            return closeBtn;
        }
    }
}
